let connection = require('../app/database');

class MsgService {
    // 分页查询信息列表
    async GetMsgListService(userID, toUserID) {
        // let statement = `select * from message limit ? offset ?;`
        // let statement = `select * from message where (toUserId = #{toUserId} and userId = #{userId}) or (toUserId = #{userId} and userId = #{toUserId});`
        let statement = `select * from message where (userID = ? && toUserID = ?) or (userID = ? && toUserID = ?);`
        let result = await connection.execute(statement, [userID,toUserID,toUserID,userID]);
        return result[0]
    };
    // 插入消息
    async InsertMsgService({userID, toUserID, msg}) {
        let statement = `INSERT INTO message (userID,toUserID,msg) VALUES (?,?,?)`
        // userID,toUserID,message
        let result = await connection.execute(statement, [userID, toUserID, msg]);
        let end = result[0].affectedRows
        if(end){
            console.log('insert msg success!')
        }else{
            console.log('insert msg error!')
        }
    }

}

module.exports = new MsgService();
